<?php
/**
 * 前端缓存
 * @author GhostWei
 * @date 2012-7-16
 */
class picture_front_cache extends system_cache_file {
	
	protected $lang = 'en_us';
	
	public function __construct() {
		$this->lang = strtolower(system_nav_language::getInstance()->getName());
		$this->_options['expire'] = system_session::getOptions('cache_expire');
		$this->_options['dir'] = _DIRECTORY_VAR . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR . $this->lang;
		$this->_options['suffix'] = 'txt';
		parent::__construct($this->_options);
	}
	
	public function __call($method, array $args) {
		if(preg_match('/^get(\w+?)$/', $method, $matches)) {
			$_id = strtolower(trim(preg_replace('/([A-Z]{1})/sU', '-$1', $matches[1]), '-'));
			$_method = 'query' . $matches[1];
			if(isset($args[0]) && isset($args[0]['id'])) {
				$_id .= '-' . urlencode($args[0]['id']);
			}
			if(!($data = $this->load($_id))) {			
				$data = $this->$_method(isset($args[0]) ? $args[0] : array());
				$this->save($_id, $data);
			}
			return $data;
		}
		return null;
	}
	
	protected function queryCategory($args = array()) {
		$name = isset($args['name']) ? $args['name'] : null;
		if(!$name) return null;	
		$concept = new picture_category_concept($this->lang);
		return $concept->fetch(array('name = ?' => $name))->toArray();
	}
	
	protected function queryNavigateCategoryList() {
		$concept = new picture_category_concept($this->lang);
		return $concept->fetchAll(array('category.is_active = 1 AND category.parent_id = 37964'), 'category.display_order ASC')->toArray();
	}
	
	protected function queryNavigateKeywordList() {
		$orderby = array('hit_total', 'hit_day', 'hit_week', 'hit_month');
		$orderIndex = $orderby[rand(0, count($orderby) - 1)];
		$concept = new resource_keyword_concept();
		$sql1 = "SELECT `keyword_id` FROM `picture_special_keyword` WHERE `special_id` IN(
					SELECT a.`id` FROM `picture_special` AS a INNER JOIN `picture_special_locale` AS b 
					ON a.`id` = b.`special_id` 
					WHERE a.`is_active` = '1' AND b.`lang` = '{$this->lang}')";
		$sql2 = "SELECT `keyword_id` FROM `picture_photo_keyword` WHERE `photo_id` IN(
					SELECT a.`id` FROM `picture_photo` AS a INNER JOIN `picture_photo_locale` AS b 
					ON a.`id` = b.`photo_id` 
					WHERE a.`is_active` = '1' AND b.`lang` = '{$this->lang}')";
		return $concept->fetchAll("id IN($sql1) OR id IN($sql2)", array("$orderIndex DESC"), 15)->toArray();
	}
	
	protected function queryIndexRecommendSpecial($args = array()) {
		$limit = isset($args['limit']) ? $args['limit'] : 48;	
		$concept = new picture_special_concept($this->lang);
		return $concept->fetchAll("special.is_active = 1 AND special.cover IS NOT NULL", array("special.display_order DESC"), $limit)->toArray();
	}
	
	
	
}
